var rounding=function (count,times,bit) {
	return parseFloat(Number(count*times).toFixed(bit));
}
var bmap;var myChart;var bmaps;
var detailsInf=function (data) {
	console.log(data);
	var marketPage=data.rows.MarketPage;
	var mediapage = data.rows.MediaPage;
	var maxX_bd=[],maxY_bd=[],minX_bd=[],minY_bd=[],cellcolor=[],hour_all_detail=[],work_hour_all_detail=[];
	var startPoint=null;
	myChart = echarts.init(document.getElementById('map'));
	if (sessionStorage.getItem('fnid')) {
		startPoint= {
			x:mediapage.grid_list[0].center_bd_lng,
			y:mediapage.grid_list[0].center_bd_lat
		}
		$('.back').append('<i class="planBackIcon"></i><span class="planBack">方案详情</span>')
		$('.media_title').html('栅格'+mediapage.grid_list[0].fnidName);
	}else if(sessionStorage.getItem('coordIng')&&sessionStorage.getItem('coordLat')){
		console.log('yesy');
		startPoint={
			x:sessionStorage.getItem('coordIng'),
			y:sessionStorage.getItem('coordLat')
		}

	}else{
		startPoint = {
			x: 116.406281,
			y: 39.916485
		};
		$('.back .planBack').remove();
		$('.back .planBackIcon').remove();
	}
// 地图自定义样式
bmap = {
	center: [startPoint.x, startPoint.y],
	zoom: 12,
	roam: 'move',
	mapStyle:{styleJson:[
		{
			"featureType": "background",
			"elementType": "all",
			"stylers": {
				"color": "#2d3039"
			}
		},
		{
			"featureType": "poi",
			"elementType": "labels.icon",
			"stylers": {
				"color": "#3d85c6"
			}
		},
		{
			"featureType": "highway",
			"elementType": "all",
			"stylers": {
				"visibility": "off"
			}
		},
		{
			"featureType": "local",
			"elementType": "all",
			"stylers": {
				"lightness": -76,
				"saturation": -77
			}
		},
		{
			"featureType": "arterial",
			"elementType": "all",
			"stylers": {
				"color": "#d0e0e3",
				"lightness": -73,
				"saturation": -77
			}
		}
		]}
	}
	if (sessionStorage.getItem('fnid')) {
		bmap.zoom=15;
	}else if (sessionStorage.getItem('zoom')) {
		bmap.zoom=sessionStorage.getItem('zoom');
	}
	var series=[];
	option = {
		bmap: bmap,
		color: ['gold', 'aqua', 'lime'],
		backgroundColor: '#404a59',
		tooltip: {
			trigger: 'item'
		},
		geo: {
			map: 'bmap',
			polyline: true,
			progressiveThreshold: 500,
			progressive: 200,
			scaleLimit:0.1,
			boundingCoords:[
			[116.832025, 40.316485],
			[116.027143, 39.772348]
			],
			label: {
				emphasis: {
					show: false
				}
			},
			roam: 'move',
			itemStyle: {
				normal: {
					areaColor: '#323c48',
					borderColor: '#404a59'
				},
				emphasis: {
					areaColor: '#2a333d'
				}
			}
		},
		series: series
	};
	myChart.setOption(option);
// 添加百度地图插件

bmaps = myChart.getModel().getComponent('bmap').getBMap();
/**
 * 获取坐标、zoom切换页面时坐标、zoom不变
 */
 $('.databtn_wrp a').on('click',function () {
 	sessionStorage.setItem('coordIng',bmaps.getCenter().lng);
 	sessionStorage.setItem('coordLat',bmaps.getCenter().lat);
 	sessionStorage.setItem('zoom',bmap.zoom);
 	// console.log(bmap.zoom);
 })
 bmaps.setMinZoom(12);
// 文字
var circle=null,label=null, opts=null,label=null;
var circleArr=[];
var ba="";
var baLabel="";
$('.trade').on('click', 'li', function() {
	$(this).toggleClass('cur').siblings('li').removeClass('cur');
	var tradingindex = $(this).index()
	var allOverlay = bmaps.getOverlays();
	if ($(this).hasClass('cur')) {
		for (var i = 0; i < allOverlay.length; i++) {
			if(ba==allOverlay[i].ba){
				bmaps.removeOverlay(allOverlay[i]);
			}
			if(baLabel==allOverlay[i].ba){
				bmaps.removeOverlay(allOverlay[i]);
			}
		}
		var point = new BMap.Point(tradingpoint[tradingindex][0], tradingpoint[tradingindex][1]);
		circle = new BMap.Circle(point, 10000, {
			strokeColor: "rgb(34,106,184)",
			strokeWeight: 2,
			strokeOpacity: 0.5,
			fillColor: 'rgba(34,106,184,0.28'
		}); 
		opts = {
			position: point, 
			offset: new BMap.Size(-40, 0), 
		}
		label = new BMap.Label(TradingArea[tradingindex].name, opts); 
		label.setStyle({
			color: "#12a8ff",
			fontSize: "21px",
			fontWeight: 'bold',
                // height : "50px",
                // lineHeight : "20px",
                fontFamily: "微软雅黑",
                width: "400px",
                // borderRadius:"100%",
                wordWrap: "break-word",
                background: "none",
                border: "none",
                whiteSpace: "pre-wrap"
            });
		bmaps.addOverlay(circle);
		bmaps.addOverlay(label);
		ba=circle.ba;
		baLabel=label.ba;
	} else {
		for (var i = 0; i < allOverlay.length; i++) {
			if(ba==allOverlay[i].ba){
				bmaps.removeOverlay(allOverlay[i]);
			}
			if(baLabel==allOverlay[i].ba){
				bmaps.removeOverlay(allOverlay[i]);
			}
		}
            // bmap.clearOverlays();
        }
    })

 // $('.trade').on('click', 'li', function() {
 //        $(this).toggleClass('cur').siblings('li').removeClass('cur');
 //        var tradingindex = $(this).index()
 //        var allOverlay = bmap.getOverlays();

 //        // console.log(tradingpoint[tradingindex]);
 //        if ($(this).hasClass('cur')) {
 //            // 
 //            for (var i = 0; i < allOverlay.length; i++) {
 //                if(ba==allOverlay[i].ba){
 //                    bmap.removeOverlay(allOverlay[i]);
 //                }
 //            }
 //            var point = new BMap.Point(tradingpoint[tradingindex][0], tradingpoint[tradingindex][1]);
 //            circle = new BMap.Circle(point, 10000, {
 //                strokeColor: "rgb(34,106,184)",
 //                strokeWeight: 2,
 //                strokeOpacity: 0.5,
 //                fillColor: 'rgba(34,106,184,0.28'
 //            }); //创建圆
 //            opts = {
 //                position: point, // 指定文本标注所在的地理位置
 //                offset: new BMap.Size(-40, 0), //设置文本偏移量
 //            }
 //            label = new BMap.Label(TradingArea[tradingindex].name, opts); // 创建文本标注对象
 //            label.setStyle({
 //                color: "#12a8ff",
 //                fontSize: "21px",
 //                fontWeight: 'bold',
 //                // height : "50px",
 //                // lineHeight : "20px",
 //                fontFamily: "微软雅黑",
 //                width: "400px",
 //                // borderRadius:"100%",
 //                wordWrap: "break-word",
 //                background: "none",
 //                border: "none",
 //                whiteSpace: "pre-wrap"
 //            });

 //            bmap.addOverlay(circle);
 //            bmap.addOverlay(label);
 //            console.log(circle)
 //            console.log(label)
 //            var allOverlay = bmap.getOverlays();
 //            console.log(allOverlay)
 //            ba=circle.ba;
 //        } else {
 //            for (var i = 0; i < allOverlay.length; i++) {
 //                if(ba==allOverlay[i].ba){
 //                    bmap.removeOverlay(allOverlay[i]);
 //                }
 //            }
 //            // bmap.clearOverlays();
 //        }
 //    })



 var ba='';
 var allcellOverlay = bmaps.getOverlays();
 console.log(allcellOverlay);
 // for(k in allcellOverlay){
 // 	// if (allcellOverlay[k]) {}
 // 	console.log(allcellOverlay[k]);
 // }
 for (var i = 0; i < allcellOverlay.length; i++) {
 	if(allcellOverlay[i].Qa==true){
 		console.log(allcellOverlay[i]);
 		bmaps.removeOverlay(allcellOverlay[i]);
 	}
 //        		// if(baLabel==allcellOverlay[i].ba){
 //        			// bmaps.removeOverlay(allcellOverlay[i]);
 //        		// }
}
var rectangleAll=[],cellcolorAll=[];
for(var k in mediapage.grid_list){
 	// console.log(k);
 	maxX_bd.push(mediapage.grid_list[k].maxX_bd);
 	maxY_bd.push(mediapage.grid_list[k].maxY_bd);
 	minX_bd.push(mediapage.grid_list[k].minX_bd);
 	minY_bd.push(mediapage.grid_list[k].minY_bd);
 	cellcolorAll.push(mediapage.grid_list[k].defaultRGB);
 	cellcolor=mediapage.grid_list[k].defaultRGB;
 	hour_all_detail.push(mediapage.grid_list[k].hour_all_detail);
 	work_hour_all_detail.push(mediapage.grid_list[k].work_hour_all_detail);
 	pStart = new BMap.Point(maxX_bd[k],maxY_bd[k]);
 	pEnd = new BMap.Point(minX_bd[k],minY_bd[k]);
 	rectangle = new BMap.Polygon([
 		new BMap.Point(pStart.lng,pStart.lat),
 		new BMap.Point(pEnd.lng,pStart.lat),
 		new BMap.Point(pEnd.lng,pEnd.lat),
 		new BMap.Point(pStart.lng,pEnd.lat)
 		], {strokeWeight:0.000001, strokeOpacity:0.00000001,fillColor:cellcolor});
 	rectangleAll.push(rectangle);
 	rectangleAll[k].find=mediapage.grid_list[k].find;
 	rectangleAll[k].addEventListener('click', function() {
 		if (!sessionStorage.getItem('fnid')) {
 			sessionStorage.setItem('fnid',this.find);
 			var SinglePARM={
 				orderResultId:sessionStorage.getItem('orderResultId'),
 				fnid:this.find,
 				orderId:sessionStorage.getItem('orderId'),
 				cityCode:sessionStorage.getItem('cityCode')   
 			}
 			getData('post',url,SingleCell,SinglePARM,detailsInf,$('body'));
 			// $('.back').append('')
 			
 		}
 		// if () {

 		// }
 	});
 	bmaps.addOverlay(rectangle);
 }
 $('.media_data #c_People').html(mediapage.handlePlan.va.countPeople);
 $('.media_data #mediaNumber').html(mediapage.handlePlan.va.mediaNumber);
 $('.data_item .vaCNum').html(rounding(mediapage.handlePlan.va.vaC,1,1));
 $('.data_item .vaRNum').html(rounding(mediapage.handlePlan.va.vaR,1,1));
 $('.data_item .vaFNum').html(rounding(mediapage.handlePlan.va.vaF,1,1));
 $('.data_item .GRPNum').html(rounding(mediapage.handlePlan.va.vaGRP,1,1));
 $('.data_item .CMPNum').html(rounding(mediapage.handlePlan.va.vaCPM,1,1));
 $('.locatinfo #location').html(mediapage.cityName);
 $('.locatinfo #Describe').html(mediapage.weather.cityDescribe);
 $('.locatinfo #Season').html(mediapage.weather.citySeason);
 $('.locatinfo #nowDate').html(mediapage.weather.nowDate);
 $('.weather .temperature').html(mediapage.weather.temperature);
 $('.weather .cond').html(mediapage.weather.cond);
 $('.weather .wind').html(mediapage.weather.wind);
 $('.weather .qlty').html(mediapage.weather.qlty);
 $('.weather .pm25').html(mediapage.weather.pm25);
 var BrandGround1='';
 var BrandGround2='';

 // marketPage.BrandGround;
 for (var k in marketPage.BrandGround){
 	BrandGround2=''
 	for (var i in marketPage.BrandGround[k].nodes){
 		BrandGround2+='<li><span>'+marketPage.BrandGround[k].nodes[i].name+'</span></li>' 
 	}
 	BrandGround1+='<li><div class="province"><i class="addopen"></i>'+marketPage.BrandGround[k].name+'<i class="mold mold1">'+marketPage.BrandGround[k].number+'</i><span class="mold_trangle "></span></div><ul class="submenu">'+BrandGround2+
 	'</ul></li>'
 }
 $('#accordion').empty();
 $('#accordion').append(BrandGround1);
 var Accordion = function(el, multiple) {
 	this.el = el || {};
 	this.multiple = multiple || false;
 	var links = this.el.find('.province');
 	var link = this.el.find('.province2');
 	links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
 	link.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
 }
 Accordion.prototype.dropdown = function(e) {
 	var $el = e.data.el;
 	$this = $(this),
 	$next = $this.next();
 	$next.stop().slideToggle();
 	var index= $this.parent().index();
 	$this.parent().toggleClass(index);
 	$this.toggleClass("downicon");
 }
 var accordion = new Accordion($('.accordion'), false);
 var  TradingArea=marketPage.TradingArea;
 var tradingpoint=[];
 $('#Format div').not('.indextitle').remove();
 $('.trade').empty();
 $('#brand div').not('.indextitle').remove();
 for (var k in TradingArea) {
 	$('.trade').append('<li><i class="circle"></i>'+TradingArea[k].name+'</li>');
 	tradingpoint.push([TradingArea[k].lng,TradingArea[k].lat]);
 }
 for (var k in marketPage.Format_business_index) {
 	$('#Format').append('<div><span>'+marketPage.Format_business_index[k].type+'</span><span>'+rounding(marketPage.Format_business_index[k].number,1,2)+'</span></div>')
 }
 for (var k in marketPage.brand_business_index) {
 	$('#brand').append('<div><span>'+marketPage.brand_business_index[k].type+'</span><span>'+marketPage.brand_business_index[k].name+'</span><span>'+rounding(marketPage.brand_business_index[k].number,1,2)+'</span></div>')
 }
 var cityIndex = Number(marketPage.cityIndex/100);
 var brandIndex =  Number(marketPage.brandIndex/100);
 var myChartliq = echarts.init(document.getElementById('liquid1'));
 var optionliq = {
 	series: [{
 		type: 'liquidFill',
 		data: [
 		{
 			value: cityIndex,
 			direction: 'left',
 			itemStyle: {
 				normal: {
 					color: '#15a5f2',
 					opacity:0.4,
 				}
 			}
 		},{
 			value: cityIndex,
 			direction: 'left',
 			itemStyle: {
 				normal: {
 					color: '#15a5f2',
 					opacity:0.4,
 				}
 			}
 		}
 		],
 		radius: '80%',
 		label: {
 			normal: {
 				formatter: function (res) {
 					return rounding(res.value,100,2);
 				},
 				textStyle: {
 					fontSize: 28,
 					color:'#fff'
 				}
 			}
 		},
 		outline: {
 			itemStyle: {
 				borderColor:'#28475e',
 			}
 		},
 		backgroundStyle:{
 			color: '#28475e'
 		},
 	}]
 };
 myChartliq.setOption(optionliq);
 var myChartliquid = echarts.init(document.getElementById('liquid2'));
 var optionliquid = {
 	series: [{
 		type: 'liquidFill',

 		data: [
 		{
 			value: brandIndex,
 			direction: 'left',
 			itemStyle: {
 				normal: {
 					color: '#15a5f2',
 					opacity:0.4,
 				}
 			}
 		},{
 			value: brandIndex,
 			direction: 'left',
 			itemStyle: {
 				normal: {
 					color: '#15a5f2',
 					opacity:0.4,
 				}
 			}
 		}
 		],
 		radius: '80%',
 		label: {
 			normal: {
 				formatter: function (res) {
 					// console.log(res);
 					return rounding(res.value,100,2);
 				},
 				textStyle: {
 					fontSize: 28,
 					color:'#fff'
 				}
 			}
 		},
 		outline: {
 			itemStyle: {
 				borderColor:'#28475e',
 			}
 		},
 		backgroundStyle:{
 			color: '#28475e'
 		},
 	}]
 };
 myChartliquid.setOption(optionliquid);

 $('.maskRight').outerHeight($('.maskLeft').outerHeight());

// marketPage.;
}
var SinglePARM={
	orderResultId:sessionStorage.getItem('orderResultId'),
	fnid:sessionStorage.getItem('fnid'),
	orderId:sessionStorage.getItem('orderId'),
	cityCode:sessionStorage.getItem('cityCode')   
}
$(function(){
	var importInf = {
		orderId:sessionStorage.getItem('orderId'),
		cityCode:sessionStorage.getItem('cityCode')
	}
	if (sessionStorage.getItem('fnid')) {
		getData('post',url,SingleCell,SinglePARM,detailsInf,$('body'));
	}else{
		getData('post',url,details,SinglePARM,detailsInf,$('body'));
	}


	$('.back').on('click','.planBack',function () {
		sessionStorage.removeItem('fnid');
		getData('post',url,details,SinglePARM,detailsInf,$('body'));

	})
})